<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>kotlin-程序结构 | litton ishir</title>
    <meta name="description" content="&quot;Someday&quot;sounds a lot like the thing people say when they actually mean &quot;Never&quot;. ">
    <link rel="icon" href="https://upload-images.jianshu.io/upload_images/1074123-dc819e68a5f3c3f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
    
    <link rel="preload" href="/assets/css/0.styles.956f79c0.css" as="style"><link rel="preload" href="/assets/js/app.b387b3a3.js" as="script"><link rel="preload" href="/assets/js/5.9a8427c2.js" as="script"><link rel="prefetch" href="/assets/js/10.89f8a53a.js"><link rel="prefetch" href="/assets/js/11.79be8c9b.js"><link rel="prefetch" href="/assets/js/2.abd3539a.js"><link rel="prefetch" href="/assets/js/3.6b6c4ca0.js"><link rel="prefetch" href="/assets/js/4.93ebd6e9.js"><link rel="prefetch" href="/assets/js/6.0cda6858.js"><link rel="prefetch" href="/assets/js/7.905ce75a.js"><link rel="prefetch" href="/assets/js/8.12136322.js"><link rel="prefetch" href="/assets/js/9.f57e2ac2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.956f79c0.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="container"><div id="head-c"><div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><h1 id="blog-name"><a href="/" class="router-link-active">
                litton ishir
            </a></h1> <a href="https://weibo.com/littonishir" target="_blank" class="social"><i class="fa fa-fw fa-weibo"></i></a><a href="https://github.com/littonishir" target="_blank" class="social"><i class="fa fa-fw fa-github"></i></a><a href="https://space.bilibili.com/268398214/" target="_blank" class="social"><i class="fa fa-fw fa-terminal"></i></a><a href="https://www.instagram.com/littonishir/" target="_blank" class="social"><i class="fa fa-fw fa-instagram"></i></a></div></div></div> <div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><div><article><p class="post-page-meta">Mar 26, 2017</p> <hr> <div class="content"><h1 id="kotlin-程序结构"><a href="#kotlin-程序结构" aria-hidden="true" class="header-anchor">#</a> kotlin-程序结构</h1> <br> <h3 id="常量与变量"><a href="#常量与变量" aria-hidden="true" class="header-anchor">#</a> 常量与变量</h3> <br> <h4 id="val"><a href="#val" aria-hidden="true" class="header-anchor">#</a> val</h4> <br> <ul><li>类似于java的final</li> <li>不能重复赋值
<br></li></ul> <h4 id="var"><a href="#var" aria-hidden="true" class="header-anchor">#</a> var</h4> <br> <ul><li>可以重复赋值.</li></ul> <h4 id="const"><a href="#const" aria-hidden="true" class="header-anchor">#</a> const</h4> <ul><li>加上 const 关键字</li> <li>程序在编译的时候就已经知道它的值了</li> <li>并且把代码中其他地方对它的引用替换成它的值</li> <li>这样可以提高代码的执行效率</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>    <span class="token keyword">const</span> <span class="token keyword">val</span> x <span class="token operator">=</span> <span class="token number">5</span> <span class="token comment">//编译期常量</span>
    <span class="token keyword">val</span> s <span class="token operator">=</span> <span class="token string">&quot;你好&quot;</span>   <span class="token comment">//运行时常量</span>
</code></pre></div><br> <h3 id="函数"><a href="#函数" aria-hidden="true" class="header-anchor">#</a> 函数</h3> <br> <h4 id="什么是函数"><a href="#什么是函数" aria-hidden="true" class="header-anchor">#</a> 什么是函数?</h4> <p>函数就是特定功能的代码块</p> <ul><li><p>fun[函数名]([参数列表]):[返回值类型]{[函数体]}</p></li> <li><p>fun[函数名]([参数列表])=[表达式]</p></li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>    <span class="token keyword">fun</span> <span class="token function">hi</span><span class="token punctuation">(</span>name<span class="token operator">:</span>String<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hi,<span class="token interpolation variable">$name</span>&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>

    <span class="token keyword">fun</span> <span class="token function">hi</span><span class="token punctuation">(</span>name<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hi,<span class="token interpolation variable">$name</span>&quot;</span><span class="token punctuation">)</span>
</code></pre></div><br> <h4 id="匿名函数"><a href="#匿名函数" aria-hidden="true" class="header-anchor">#</a> 匿名函数</h4> <br> <ul><li>没有名字的函数</li> <li>需要用一个变量来接收函数的返回</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>    <span class="token keyword">val</span> hi <span class="token operator">=</span> <span class="token keyword">fun</span><span class="token punctuation">(</span>name<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hi,<span class="token interpolation variable">$name</span>&quot;</span><span class="token punctuation">)</span>
</code></pre></div><br> <h4 id="编写函数注意事项"><a href="#编写函数注意事项" aria-hidden="true" class="header-anchor">#</a> 编写函数注意事项</h4> <br> <ul><li>功能单一</li> <li>函数名要顾名思义</li> <li>参数个数不要太多</li></ul> <hr> <br> <h3 id="lambda表达式"><a href="#lambda表达式" aria-hidden="true" class="header-anchor">#</a> Lambda表达式</h3> <br> <h4 id="什么是lambda表达式"><a href="#什么是lambda表达式" aria-hidden="true" class="header-anchor">#</a> 什么是Lambda表达式?</h4> <ul><li>匿名函数</li> <li>写法 : {(参数列表) -&gt; {函数体,最后一行是返回值}}</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>    <span class="token keyword">val</span> sum <span class="token operator">=</span> <span class="token punctuation">{</span>x<span class="token operator">:</span>Int<span class="token punctuation">,</span>y<span class="token operator">:</span>Int<span class="token operator">-&gt;</span>x<span class="token operator">+</span>y<span class="token punctuation">}</span>
</code></pre></div><h4 id="lambda的类型举例"><a href="#lambda的类型举例" aria-hidden="true" class="header-anchor">#</a> Lambda的类型举例</h4> <ul><li><p>()-&gt;Unit</p> <ul><li>无参,返回值为Unit</li></ul></li> <li><p>(Int)-&gt;Int</p> <ul><li>传入整形,返回一个整形</li></ul></li> <li><p>(String,((String)-&gt;String))-&gt;Boolean</p> <ul><li>传入字符串,Lambda表达式,返回Boolean</li></ul></li></ul> <h4 id="lambda表达式的简化"><a href="#lambda表达式的简化" aria-hidden="true" class="header-anchor">#</a> Lambda表达式的简化</h4> <ul><li>函数参数调用时最后一个Lambda可以移出去</li> <li>函数参数只有一个Lambda,调用时小括号可以省略</li> <li>Lambda只有一个参数可默认为it</li> <li>入参,返回值与形参一致的函数可以用函数引用的方式作为实参传入</li></ul> <br> <h3 id="表达式"><a href="#表达式" aria-hidden="true" class="header-anchor">#</a> 表达式</h3> <br> <h4 id="中缀表达式"><a href="#中缀表达式" aria-hidden="true" class="header-anchor">#</a> 中缀表达式</h4> <ul><li>调用时可以省去 . () 调用</li> <li>只有一个参数,且用 infix 修饰的函数</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> <span class="token function">Paper</span><span class="token punctuation">(</span>word<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> word <span class="token operator">=</span> word
<span class="token punctuation">}</span>

<span class="token keyword">class</span> Pen <span class="token punctuation">{</span>
    <span class="token keyword">infix</span> <span class="token keyword">fun</span> <span class="token function">on</span><span class="token punctuation">(</span>paper<span class="token operator">:</span> Paper<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>paper<span class="token punctuation">.</span>word<span class="token punctuation">.</span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Pen on Paper write <span class="token interpolation"><span class="token delimiter variable">${</span>paper<span class="token punctuation">.</span>word<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Pen not on Paper&quot;</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> pen <span class="token operator">=</span> <span class="token function">Pen</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">val</span> paper <span class="token operator">=</span> <span class="token function">Paper</span><span class="token punctuation">(</span><span class="token string">&quot;\&quot;Hello infix\&quot;&quot;</span><span class="token punctuation">)</span>
    pen on paper
<span class="token punctuation">}</span>
</code></pre></div><h4 id="if-表达式"><a href="#if-表达式" aria-hidden="true" class="header-anchor">#</a> if 表达式</h4> <ul><li>在kotlin中 if 不只是分支语句还是表达式 if 是有返回值的</li> <li>代码块内语句最后一行为返回值</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">val</span> s<span class="token operator">:</span>Int <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>paper<span class="token punctuation">.</span>word<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token number">0</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
        <span class="token number">1</span>
    <span class="token punctuation">}</span>
</code></pre></div><h4 id="when-表达式"><a href="#when-表达式" aria-hidden="true" class="header-anchor">#</a> when 表达式</h4> <ul><li>加强版的 switch ,支持任意类型</li> <li>支持纯表达式条件分支(类似于 if)</li></ul> <p>例一</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span>args<span class="token operator">:</span> Array<span class="token operator">&lt;</span>String<span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> x <span class="token operator">=</span> <span class="token number">5</span>
    <span class="token keyword">when</span> <span class="token punctuation">(</span>x<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">is</span> Int <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello <span class="token interpolation variable">$x</span>&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">in</span> <span class="token number">1</span><span class="token operator">..</span><span class="token number">100</span> <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;<span class="token interpolation variable">$x</span> is in 1..100&quot;</span><span class="token punctuation">)</span>
        <span class="token operator">!</span><span class="token keyword">in</span> <span class="token number">1</span><span class="token operator">..</span><span class="token number">100</span> <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;<span class="token interpolation variable">$x</span> is not in 1..100&quot;</span><span class="token punctuation">)</span>
        args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;x == args[0]&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">val</span> mode <span class="token operator">=</span> <span class="token keyword">when</span> <span class="token punctuation">{</span>
        args<span class="token punctuation">.</span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span><span class="token operator">/</span>br<span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>br<span class="token operator">&gt;</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&quot;1&quot;</span> <span class="token operator">-&gt;</span> <span class="token number">1</span>
        <span class="token keyword">else</span> <span class="token operator">-&gt;</span> <span class="token number">0</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>例二</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">class</span> Player <span class="token punctuation">{</span>
    <span class="token keyword">enum</span> <span class="token keyword">class</span> State <span class="token punctuation">{</span>
        IDLE<span class="token punctuation">,</span> BUFFERING<span class="token punctuation">,</span> PLAYING<span class="token punctuation">,</span> PAUSED
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">var</span> state <span class="token operator">=</span> State<span class="token punctuation">.</span>IDLE


    <span class="token keyword">fun</span> <span class="token function">pause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">when</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            Player<span class="token punctuation">.</span>State<span class="token punctuation">.</span>BUFFERING<span class="token punctuation">,</span> Player<span class="token punctuation">.</span>State<span class="token punctuation">.</span>PLAYING <span class="token operator">-&gt;</span> <span class="token function">doPause</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>

            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">fun</span> <span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">when</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            Player<span class="token punctuation">.</span>State<span class="token punctuation">.</span>PAUSED <span class="token operator">-&gt;</span> <span class="token function">doResume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token comment">//什么都不做</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">fun</span> <span class="token function">doResume</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> State<span class="token punctuation">.</span>PLAYING
        <span class="token comment">//处理真正的继续播放逻辑</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">private</span> <span class="token keyword">fun</span> <span class="token function">doPause</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        state <span class="token operator">=</span> State<span class="token punctuation">.</span>PAUSED
        <span class="token comment">//处理真正的暂停逻辑</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="循环语句"><a href="#循环语句" aria-hidden="true" class="header-anchor">#</a> 循环语句</h3> <br> <h4 id="for-循环"><a href="#for-循环" aria-hidden="true" class="header-anchor">#</a> for 循环</h4> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>    <span class="token keyword">val</span> listInt <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token keyword">in</span> listInt<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
</code></pre></div><h4 id="跳过和终止循环"><a href="#跳过和终止循环" aria-hidden="true" class="header-anchor">#</a> 跳过和终止循环</h4> <ul><li>continue : 跳过当前循环</li> <li>break : 终止循环</li> <li>多层循环嵌套终止结合标签使用</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code>   <span class="token keyword">val</span> listInt <span class="token operator">=</span> <span class="token function">listOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">23</span><span class="token punctuation">)</span>
    <span class="token label symbol">Outer@</span><span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token keyword">in</span> listInt<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
        <span class="token label symbol">Inner@</span><span class="token keyword">while</span> <span class="token punctuation">(</span>i<span class="token operator">&gt;</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">break</span><span class="token label symbol">@Outer</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="异常捕获"><a href="#异常捕获" aria-hidden="true" class="header-anchor">#</a> 异常捕获</h3> <br> <ul><li>try catch 用法和Java一致</li> <li>finally 无论代码是否抛异常都会执行</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code> <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">val</span> arg1 <span class="token operator">=</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//1</span>
        <span class="token keyword">val</span> arg2 <span class="token operator">=</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">toInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//2a</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;<span class="token interpolation variable">$arg1</span> + <span class="token interpolation variable">$arg2</span> = <span class="token interpolation"><span class="token delimiter variable">${</span><span class="token function">sum</span><span class="token punctuation">(</span>arg1<span class="token punctuation">,</span> arg2<span class="token punctuation">)</span><span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token operator">:</span> NumberFormatException<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;您确定输入的是整数吗?&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token operator">:</span> ArrayIndexOutOfBoundsException<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;您确定输入的是两个整数吗?&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token operator">:</span> Exception<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;程序出现了未知异常,可能是您的人品太差了.<span class="token interpolation"><span class="token delimiter variable">${</span>e<span class="token punctuation">.</span>message<span class="token delimiter variable">}</span></span>&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token keyword">finally</span> <span class="token punctuation">{</span>
        <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;谢谢您使用我们的加法计算器&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="具名参数"><a href="#具名参数" aria-hidden="true" class="header-anchor">#</a> 具名参数</h3> <br> <p>具名参数 : 给函数的实参附上形参</p> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">sum</span><span class="token punctuation">(</span>args1<span class="token operator">:</span>Int<span class="token punctuation">,</span>args2<span class="token operator">:</span>Int<span class="token punctuation">)</span><span class="token operator">=</span>args1<span class="token operator">+</span>args2
<span class="token function">sum</span><span class="token punctuation">(</span>args1<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span>args2<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token comment">//此时args2=3在前在后编译器都知道3是给args2的</span>
</code></pre></div><br> <h3 id="变长参数"><a href="#变长参数" aria-hidden="true" class="header-anchor">#</a> 变长参数</h3> <br> <ul><li>变长参数可以接收多个值</li> <li>可以在参数列表的任意位置</li> <li>若传参有歧义时 需用具名参数</li></ul> <div class="language-kotlin extra-class"><pre class="language-kotlin"><code><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">vararg</span> args<span class="token operator">:</span> String<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">val</span> list <span class="token operator">=</span> <span class="token function">arrayListOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
    <span class="token keyword">val</span> array <span class="token operator">=</span> <span class="token function">intArrayOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
    <span class="token comment">//*array 目前只支持数组 只用于变长参数列表展开数组</span>
    <span class="token function">hello</span><span class="token punctuation">(</span><span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token operator">*</span>array<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">hello</span><span class="token punctuation">(</span>double<span class="token operator">:</span> Double<span class="token punctuation">,</span> <span class="token keyword">vararg</span> ints<span class="token operator">:</span> Int<span class="token punctuation">,</span> string<span class="token operator">:</span> String <span class="token operator">=</span> <span class="token string">&quot;Hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">println</span><span class="token punctuation">(</span>double<span class="token punctuation">)</span>
    ints<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token operator">::</span>println<span class="token punctuation">)</span>
    <span class="token function">println</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><br> <h3 id="默认参数"><a href="#默认参数" aria-hidden="true" class="header-anchor">#</a> 默认参数</h3> <br> <ul><li>为函数参数指定默认值</li> <li>可以为任意位置的参数指定默认值</li> <li>若传参有歧义时 需用具名参数</li></ul></div></article> <!----></div></div></div> <center><hr width="0%"> <span id="subtitle">&quot;Someday&quot;sounds a lot like the thing people say when they actually mean &quot;Never&quot;. </span> <div class="row"><div class="offset-lg-2 col-lg-8 offset-md-1 col-md-10"><p class="small">© 2019 litton ishir. Code released under the <a href="https://opensource.org/licenses/MIT" target="_blank">MIT License</a></p></div></div></center></div></div>
    <script src="/assets/js/app.b387b3a3.js" defer></script><script src="/assets/js/5.9a8427c2.js" defer></script>
  </body>
</html>
